﻿using BlazorOrdering.Core;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorOrdering.Web.Entry.Pages
{
    public partial class OrderStatementCard
    {
        [Parameter]
        public List<BoMenu> boMenus { get; set; } 

        [Parameter]
        public OrderStatement orderStatement { get; set; }

        [Parameter]
        public EventCallback<OrderStatement> orderStatementChanged { get; set; }

        [Parameter]
        public EventCallback<MouseEventArgs> OnClickCallback { get; set; }//调用父组件的计算合计

        [Parameter]
        public EventCallback<string> CloseOneselfClickCallback { get; set; }//调用父组件关闭自己
        //    [Parameter]
        //    public [DataType][PropertyName] { get; set; }
        //      [Parameter]
        //      public EventCallback<[DataType]>[PropertyName] Changed { get; set; }


        private  List<SelectedItem> Types = new();
        //private  List<SelectedItem> Names = new();
        private List<SelectedItem> _names = new List<SelectedItem>();
        private Select<string>? SelectName { get; set; }
        private IEnumerable<SelectedItem> Items2 => _names;

        private bool DownIsDisable { get; set; } = false;
        private bool UpIsDisable { get; set; } = false;

        protected override void OnInitialized()
        {

            // 初填充Type 下拉框的选项
            var query = boMenus.GroupBy(x => x.Type)
                                .Select(y => y.Key)
                                .ToList();
            foreach (var item in query)
            {
                Types.Add(new SelectedItem(item, item));
            }



            base.OnInitialized();
        }



        private void changnumber()
        {
            this.orderStatement.subtotal = 20;
        }


        /// <summary>
        /// type选择变更后
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private Task OnCascadeBindSelectClick(SelectedItem item)
        {
            //将type 赋值给orderStatement.Type
            orderStatement.Type = item.Value;
            //清空现有的 菜名选项
            _names.Clear();
            //按照类型进行筛选
            var query = boMenus.Where(m => m.Type == item.Value).ToList();
            foreach (var q in query)
            {
                _names.Add(new SelectedItem(q.Name, q.Name));
            }

            SelectName?.SetItems(_names);
            //这里要默认选中第一个菜名
            _ = OnNameChanged(_names[0]);
            return Task.CompletedTask;
        }

        //选中菜名后将细项赋值
        private async Task<Task> OnNameChanged(SelectedItem item)
        {
            //菜名选择后,按照菜名筛选菜单
            var setlectMeun = boMenus.Where(m => m.Name == item.Value).ToList();
            if (setlectMeun.Count > 0)
            {
                this.orderStatement.Day = setlectMeun[0].Day;
                this.orderStatement.Type = setlectMeun[0].Type;
                this.orderStatement.Name = setlectMeun[0].Name;
                this.orderStatement.Price = setlectMeun[0].Price;
                this.orderStatement.Number = 1;
                this.orderStatement.subtotal = this.orderStatement.Price * this.orderStatement.Number;
                await OnClickCallback.InvokeAsync();//调用父组件的方法，计算订单的合计
            }
            await InvokeAsync(StateHasChanged);
            
            return Task.CompletedTask;
        }

        private async Task<Task> ButtonDownClick(MouseEventArgs e)
        {
            if (orderStatement.Number>1)
            {

            this.orderStatement.Number = orderStatement.Number - 1;
            this.orderStatement.subtotal = this.orderStatement.Price * this.orderStatement.Number;
            await OnClickCallback.InvokeAsync();//调用父组件的方法，计算订单的合计
            }
            else
            {
                //禁用按钮
                DownIsDisable = true;
                StateHasChanged();
            }
            //StateHasChanged();
            return Task.CompletedTask;
        }

        private async Task<Task> ButtonUpClick(MouseEventArgs e)
        {
            if (this.orderStatement.Number<98)
            {

            this.orderStatement.Number = orderStatement.Number + 1;
            this.orderStatement.subtotal = this.orderStatement.Price * this.orderStatement.Number;
                if (orderStatement.Number>1 && DownIsDisable)
                {
                    DownIsDisable = false;
                }
            //StateHasChanged();
            await OnClickCallback.InvokeAsync();//调用父组件的方法，计算订单的合计
            }
            else
            {
                UpIsDisable = true;
                StateHasChanged();
            }
            return Task.CompletedTask;
        }

        private async Task CloseOneselfClick()
        {
            await CloseOneselfClickCallback.InvokeAsync(orderStatement.Name);
        }


    }
}
